/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.invoice.customer.controller;

import javax.validation.Valid;
import com.google.common.collect.Lists;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.aop.logging.annotation.ApiLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.invoice.customer.service.dto.YxfpCustomerDTO;
import com.jeeplus.invoice.customer.service.mapstruct.YxfpCustomerWrapper;
import com.jeeplus.invoice.customer.service.YxfpCustomerService;

/**
 * 客户管理Controller
 * @author 张如利
 * @version 2022-01-20
 */

@Api(tags ="客户管理")
@RestController
@RequestMapping(value = "/invoice/customer/yxfpCustomer")
public class YxfpCustomerController {

	@Autowired
	private YxfpCustomerService yxfpCustomerService;

	@Autowired
	private YxfpCustomerWrapper yxfpCustomerWrapper;

	/**
	 * 客户管理列表数据
	 */
	@ApiLog("查询客户管理列表数据")
	@ApiOperation(value = "查询客户管理列表数据")
	@PreAuthorize("hasAuthority('invoice:customer:yxfpCustomer:list')")
	@GetMapping("list")
	public ResponseEntity<IPage<YxfpCustomerDTO>> list(YxfpCustomerDTO yxfpCustomerDTO, Page<YxfpCustomerDTO> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (yxfpCustomerDTO, YxfpCustomerDTO.class);
		IPage<YxfpCustomerDTO> result = yxfpCustomerService.findPage (page, queryWrapper);
		return ResponseEntity.ok (result);
	}


	/**
	 * 根据Id获取客户管理数据
	 */
	@ApiLog("根据Id获取客户管理数据")
	@ApiOperation(value = "根据Id获取客户管理数据")
	@PreAuthorize("hasAnyAuthority('invoice:customer:yxfpCustomer:view','invoice:customer:yxfpCustomer:add','invoice:customer:yxfpCustomer:edit')")
	@GetMapping("queryById")
	public ResponseEntity<YxfpCustomerDTO> queryById(String id) {
		return ResponseEntity.ok ( yxfpCustomerService.findById ( id ) );
	}

	/**
	 * 保存客户管理
	 */
	@ApiLog("保存客户管理")
	@ApiOperation(value = "保存客户管理")
	@PreAuthorize("hasAnyAuthority('invoice:customer:yxfpCustomer:add','invoice:customer:yxfpCustomer:edit')")
	@PostMapping("save")
	public  ResponseEntity <String> save(@Valid @RequestBody YxfpCustomerDTO yxfpCustomerDTO) {
		//新增或编辑表单保存
		yxfpCustomerService.saveOrUpdate (yxfpCustomerWrapper.toEntity (yxfpCustomerDTO));
        return ResponseEntity.ok ( "保存客户管理成功" );
	}


	/**
	 * 删除客户管理
	 */
	@ApiLog("删除客户管理")
	@ApiOperation(value = "删除客户管理")
	@PreAuthorize("hasAuthority('invoice:customer:yxfpCustomer:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        yxfpCustomerService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除客户管理成功" );
	}

	/**
	 * 通过微信OpenID获取客户信息
	 * @param openId
	 */
	@ApiOperation("通过客户微信OpenID获取客户信息")
	@GetMapping("queryByOpenId")
	public ResponseEntity<YxfpCustomerDTO> getCustomerByOpenId(String openId){
		return ResponseEntity.ok(yxfpCustomerService.findByOpenId(openId));
	}
}
